package com.fjec.devops.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.devops.entity.StatAllUserNum;
import com.fjec.devops.entity.StatMarketUserNum;
import com.fjec.devops.entity.StatUnmarketUserNum;
import com.fjec.devops.mapper.StatUnmarketUserNumMapper;
import com.fjec.devops.service.IStatUnmarketUserNumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.devops.view.MarketUserNumVO;
import com.fjec.devops.view.UnMarketUserNumVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 016.统计-普通用户数量表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-09-18
 */
@Service
public class StatUnmarketUserNumServiceImpl extends ServiceImpl<StatUnmarketUserNumMapper, StatUnmarketUserNum> implements IStatUnmarketUserNumService {

    @Resource
    private StatUnmarketUserNumMapper mapper;

    @Override
    public JsonResult statUnMarketUserNum(Integer days) throws Exception {
        // 通过days计算开始日期和结束日期
        LocalDate endTime = DateUtil.nowLd();
        LocalDate startTime = DateUtil.ldMinus(endTime, ChronoUnit.DAYS, days);
        List<StatUnmarketUserNum> entityList = mapper.statUnmarketUserData(startTime, endTime);
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (StatUnmarketUserNum entity : entityList) {
            xList.add(DateUtil.format(entity.getStatDate(), "MM月dd日"));
            yList.add(entity.getUserNum());
        }
        UnMarketUserNumVO view = new UnMarketUserNumVO();
        view.setXList(xList);
        view.setYList(yList);
        return JsonResult.success()
                .data(view)
                .build();
    }

    @Override
    public JsonResult statUnMarketUserFakeData() throws Exception {
        LocalDateTime now = LocalDateTime.now();
        int before = 365;
        double rate = 0;
        int counter = 0;
        for (int i = 0; i < before; i++) {
            StatUnmarketUserNum entity = new StatUnmarketUserNum();
            LocalDateTime dt = now.minusDays(before - i);
            rate = Math.random();
            if (dt.getDayOfWeek().equals("FRIDAY")) {
                rate = rate * 1.3;
            }
            if (dt.getDayOfWeek().equals("SATURDAY")) {
                rate = rate * 2.3;
            }
            if (dt.getDayOfWeek().equals("SUNDAY")) {
                rate = rate * 3.3;
            }
            counter = counter + (int) Math.floor(3 * (rate + 0.5) * 64 * i);
            entity.setUserNum(counter);
            entity.setStatDate(dt.toLocalDate());
            entity.setUpdateTime(dt);
            entity.setCreateTime(dt);
            // 这样会不会数据库压力太大
            mapper.insert(entity);
        }
        return JsonResult.success().build();
    }
}
